﻿<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
      <router-link
        v-if="checkPermission(['SysDictionaryManagement.SysDictionary.Create'])"
        :to="'/systemManagement/sysDictionary-create'"
      >
        <el-button class="filter-item" type="primary" icon="el-icon-plus">
          添加
        </el-button>
      </router-link>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      style="width: 100%"
      :data="list"
      @sort-change="sortChange"
    >
      <el-table-column label="字典编码" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.dictCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="字典名称" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.dictName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="序号" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.sortNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" width="110px" align="center">
        <template slot-scope="{row}">
          <el-tag
            :type="row.isEnabled | isEnabledFilter"
          >
            {{ row.isEnabled ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row}">
          <router-link :to="'/systemManagement/sysDictionary-preview/'+row.id">
            <el-button type="success" size="mini" icon="el-icon-view">
              查看
            </el-button>
          </router-link>
          <router-link
            v-if="checkPermission(['SysDictionaryManagement.SysDictionary.Update'])"
            :to="'/systemManagement/sysDictionary-edit/'+row.id"
          >
            <el-button type="primary" size="mini" icon="el-icon-edit">
              编辑
            </el-button>
          </router-link>
          <el-button v-if="checkPermission(['SysDictionaryManagement.SysDictionary.Delete'])" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getListPaged, remove } from '@/api/sysDictionary'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import checkPermission from '@/utils/permission' // 权限判断函数

export default {
  name: 'SysDictionary',
  components: { Pagination },
  directives: { waves },
  filters: {
    isEnabledFilter(isEnabled) {
      if (isEnabled) {
        return 'success'
      } else {
        return 'warning'
      }
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        name: undefined,
        sorting: 'id'
      },
      dialogFormVisible: false,
      downloadLoading: false,
      loading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true
      const params = {
        filter: this.listQuery.name,
        sorting: this.listQuery.sorting,
        skipCount: (this.listQuery.page - 1) * this.listQuery.limit,
        maxResultCount: this.listQuery.limit
      }
      getListPaged(params).then(response => {
        this.list = response.data.items
        this.total = response.data.totalCount
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'creationTime') {
        this.sortByCreationTime(order)
      }
    },
    sortByCreationTime(order) {
      if (order === 'ascending') {
        this.listQuery.sorting = 'creationTime'
      } else {
        this.listQuery.sorting = 'creationTime desc'
      }
      this.handleFilter()
    },
    handleDelete(row) {
      this.$confirm('此操作将删除该, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        const { id } = row
        remove(id).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      }).catch(() => {
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
        const data = this.formatJson(filterVal, this.list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}`
        ? 'ascending'
        : sort === `-${key}`
          ? 'descending'
          : ''
    }
  }
}
</script>
